package com.easywan.util;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtils {  
	
	public static void sendEmail(String to,String title,String messageText){
		MimeMessage message = null;
		final String from = "18476682462@163.com";
		 String host = "smtp.163.com";
		 final String pwd = "mail18476682462";
		 Properties properties = System.getProperties();
		
		// 设置邮件服务器
	      properties.setProperty("mail.smtp.host", host);
	      properties.put("mail.smtp.auth", "true");
	   // 获取默认session对象
	      Session session = Session.getDefaultInstance(properties,new Authenticator(){
	        public PasswordAuthentication getPasswordAuthentication()
	        {
	         return new PasswordAuthentication(from, pwd); //发件人邮件用户名、密码
	        }
	        
	       });
	       message = new MimeMessage(session);
	      // Set From: 头部头字段
	         try {
				message.setFrom(new InternetAddress(from));
			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        // Set To: 头部头字段
        try {
        	message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
			//message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
	        // Set Subject: 头部头字段
	         message.setSubject(title);
	         message.setText(messageText);
	         Transport.send(message);
        } catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}